package com.oa.hyperion.api.v1;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.hyperion.entity.Employee;
import com.oa.hyperion.entity.Post;
import com.oa.hyperion.entity.Resume;
import com.oa.hyperion.exception.http.NotFoundException;
import com.oa.hyperion.service.PostService;
import com.oa.hyperion.service.ResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


/**
 * 简历controller
 */

//使用RestController =controller+ ResponseBody
@Api(value="简历controller",tags={"简历操作接口"})
@RestController
@RequestMapping("/v1/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    /**
     * 获取所有简历
     */
    @ApiOperation("获取所有简历")
    @GetMapping("getAll")
    public List<Resume> getAll(){
        return resumeService.list();
    }


    /**
     * 分页查询简历
     */
    @ApiOperation("分页查询所用简历")
    @GetMapping("getAllByPage")
    public Page<Resume> getAllByPage(Integer pageNum, Integer pageSize){
        //创建Page对象
        Page<Resume> employeePage = new Page<>(pageNum,pageSize);
        return resumeService.page(employeePage);
    }

    /**
     * 查询单个简历
     *  发送者名字，id，
     */
    @ApiOperation("查询单个简历")
    @GetMapping("getOne")
    public Resume getOneByCondition(@RequestBody Resume resume){
        QueryWrapper<Resume> wrapper=new QueryWrapper<>();
        //以id来查找
        if(resume.getId()!=null){
            wrapper.eq("id",resume.getId());
        }
        //以name来查找
        if(resume.getSender()!=null){
            wrapper.eq("sender",resume.getSender());
        }
        return resumeService.getOne(wrapper);
    }


    /**
     * 添加简历
     */
    @ApiOperation("添加简历")
    @PostMapping("add")
    public String addResume(@RequestBody Resume resume){
        resume.setAcceptTime(new Timestamp(new Date().getTime()));

        if(resumeService.save(resume)) {
            return "添加成功";
        }
        return "添加失败";
    }


    /**
     * 逻辑删除,简历
     */
    @ApiOperation("逻辑删除,简历")
    @DeleteMapping("delete")
    public String deletePost(Integer id){
        Resume resume=new Resume();
        resume.setId(id);
        resume.setStatus("n");

        if(resumeService.updateById(resume)) {
            return "删除成功";
        }
        return "删除失败";

    }

    /**
     * 修改简历
     */
    @ApiOperation("修改简历")
    @PutMapping("update")
    public String updatePost(@RequestBody Resume resume){
        //获取要修改的对象
        Resume r=resumeService.getById(resume.getId());

             //有效性校验
            if(r==null){
                throw new NotFoundException(404);
            }

        //修改信息
        if(r.getSender()!=null){
            r.setSender(resume.getSender());
        }

        if(r.getEducation()!=null){
            r.setEducation(resume.getEducation());
        }
        if(r.getAcceptTime()!=null){
            r.setAcceptTime(resume.getAcceptTime());
        }

        if(r.getAffix()!=null){
            r.setAffix(resume.getAffix());
        }

        if(r.getStatus()!=null){
            r.setStatus(resume.getStatus());
        }


        if( resumeService.updateById(r)){
            return "修改成功";
        }
        return "修改失败";
    }

}
